package utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import forms.MainForm;

import java.io.File;
import java.util.HashMap;

public class SettingUtil {
    private static final String TAG = "SettingUtil";
    public static final String USE_VIDEO_STREAM = "useVideoStream"; // 是否使用视频流投屏模式
    private static final File configFile = new File(System.getProperty("user.dir"), "HOScrcpyConfig.json");
    private static MainForm mainForm;
    private static HashMap<String, String> SCREEN_CAPTURE_PARAM_MAPPER = new HashMap<>();
    private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    private static boolean useVideoStream = true;

    public static void saveConfig() {
        try {
            ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
            objectNode.put(USE_VIDEO_STREAM, useVideoStream);
            FileUtil.createFileByString(configFile, objectNode.toString());
        } catch (Exception ex) {
            Log.info(TAG, "save config file error: " + ex.getMessage());
        }
    }

    public static void setUseVideoStream(boolean useVideoStream) {
        SettingUtil.useVideoStream = useVideoStream;
    }

    public static boolean useVideoStream() {
        String value = getSaveSettingByKey(USE_VIDEO_STREAM);
        if (value == null) {
            return true;
        }
        return value.equals("true");
    }

    public static String getSaveSettingByKey(String key) {
        if (configFile.exists()) {
            try {
                JsonNode jsonNode = OBJECT_MAPPER.readTree(configFile);
                if (jsonNode.get(key) != null) {
                    return jsonNode.get(key).asText().trim();
                }
            } catch (Exception ex) {
                Log.info(TAG, "parse config file error:" + ex);
            }
        }
        return null;
    }
}
